<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/1
 * Time: 15:20
 */

namespace app\adminapiv3\controller;
use app\adminapiv3\service;
use think\Request;

//授权管理
class ServerAuth extends Base
{
    /**
     * @name 授权列表
     * @author wx
     * @date 2019/11/6 16:54
     */
    public function list()
    {
        $params['product_name'] = $this->filterInput('product_name','get');
        $params['product_m_version'] = $this->filterInput('product_m_version','get');
        $params['number'] = $this->filterInput('number','get');
        $params['stime'] = $this->filterInput('stime','get');
        $params['etime'] = $this->filterInput('etime','get');

        $server = new \app\adminapiv3\service\ServerAuth();

        $list = $server->list($params, $this->getLimit());

        $this->jsonOutPut($list);
    }

    /**
     * @name 在线注册
     * @author wx
     * @date 2019/11/6 16:55
     */
    public function online()
    {
        global $logContent;
        $logContent = '在线注册';

        $key = $this->filterInput('key*');

        $authServer = new \app\adminapiv3\service\ServerAuth();
        $authServer->online($key);

        $this->jsonOutPut();
    }

    /**
     * @name 离线注册
     * @author wx
     * @date 2019/11/6 16:56
     */
    public function offline()
    {
        global $logContent;
        $logContent = '离线注册';

        $file = request()->file('file');

        if (!$file) {
            returnErrJson(122,'请上传授权文件');
        }

        $authServer = new \app\adminapiv3\service\ServerAuth();
        $authServer->offline($file);

        $this->jsonOutPut();
    }

    /**
     * @name: 平台授权信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-20 17:43:49
     */
    function platformAuthInfo ()
    {
        $ServerAuth = new service\ServerAuth();
        $data = $ServerAuth->platformAuthInfo();

        $this->jsonOutPut($data);
    }

    /**
     * @name 首页授权信息展示
     * @param Request $request
     * @author wx
     * @date 2020/11/13 12:44
     */
    public function userAuthInfo(Request $request)
    {
        $accesstoken = $request->accesstoken;

        $ServerAuth = new service\ServerAuth();
        $data = $ServerAuth->userAuthInfo($accesstoken);

        $this->jsonOutPut($data);
    }


    /**
     * @name 下载hostkey
     * @author wx
     * @date 2020/5/28 9:48
     */
    function getHostFile()
    {
        $data['ip'] = $_SERVER['SERVER_ADDR'];
        $data['port'] = $_SERVER['SERVER_PORT'];
        $data['path'] = 'static/host/hostkey.key';
        $data['url'] = $_SERVER['REQUEST_SCHEME'].'://' . $_SERVER['HTTP_HOST']  . '/static/host/hostkey.key';

        $server = new \app\adminapiv3\service\ServerAuth();
        $server->createHostFile();

        $this->jsonOutPut($data);
    }
}